package com.hl.card.modular.pack.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hl.card.common.persistence.model.User;
import com.hl.card.modular.pack.dao.DisMemberInfoMapper;
import com.hl.card.modular.pack.model.DisMemberInfoModel;
import com.hl.card.modular.pack.service.DisMemberInfoYService;
import com.hl.card.modular.pack.service.UserDataService;
import com.hl.card.common.util.DateUtils;
import com.hl.card.common.util.HttpURLClient;
import com.hl.card.common.util.ResponseData;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DisMemberInfoServiceYImp  implements DisMemberInfoYService{
    	@Resource(name="disMemberInfoYMapper")
    	private DisMemberInfoMapper disMemberInfoMapper;
    	@Autowired
    	private ResponseData responseData;
		@Autowired
		private UserDataService userDataService;

    	@Value("${dismemberapply.ka_ip}")
    	private String ip;  //卡包项目IP

		@Override
		public ResponseData queryDisMemberInfoByParentId(String parent_id,Integer start,Integer size) {
			Map<String,Object> map=new HashMap<String, Object>();
			map.put("start", start);
			map.put("size", size);
			map.put("user_id", parent_id);
			List<DisMemberInfoModel> list=disMemberInfoMapper.queryDisMemberInfoByParentId(map);
			int count=disMemberInfoMapper.queryDisMemberInfoByParentIdCount(parent_id);
			Map<String,Object> result=new HashMap<String, Object>();
			result.put("data", list);
			result.put("people_count", count);
			responseData.setData(result);
			responseData.setMsg("操作成功");
			responseData.setStatus("1");
			return responseData;
		}

		@Override
		public ResponseData queryUserInfoByParentId(String parent_id,Integer start,Integer size) {
			Map<String,Object> map=new HashMap<String, Object>();
			map.put("start", start);
			map.put("size", size);
			map.put("user_id", parent_id);

			Wrapper<User> wrapper1 = new EntityWrapper<>();
			wrapper1.eq("user_id", parent_id);
			User newUser = userDataService.selectOne(wrapper1);
			String regCode = newUser.getReg_code();
			System.out.println("用户ID：" + newUser.getUserId());
			System.out.println("用户账号：" + newUser.getAccount());
			System.out.println("邀请注册码：" + regCode);
			if(StringUtils.isNotBlank(regCode)) {
				map.put("refer_reg_code", regCode);
				List<DisMemberInfoModel> list=disMemberInfoMapper.queryUserInfoByParentId(map);
				int count=list.size();//disMemberInfoMapper.queryUserInfoByParentIdCount(parent_id);
				int order_count=0;
				Map<String,Object> result=new HashMap<String, Object>();
				result.put("data", list);
				result.put("people_count", count);
				result.put("order_count", order_count);
				responseData.setData(result);
				responseData.setMsg("操作成功");
				responseData.setStatus("1");
				return responseData;
			}else {
				List<DisMemberInfoModel> list = null;
				int count= 0;
				int order_count = 0;
				Map<String,Object> result=new HashMap<String, Object>();
				result.put("data", list);
				result.put("people_count", count);
				result.put("order_count", order_count);
				responseData.setData(result);
				responseData.setMsg("操作成功");
				responseData.setStatus("1");
				return responseData;
			}
		}

		@Override
		public ResponseData queryMyEarnings(String user_id, Integer start,
				Integer size, Integer flag,String cardType) {
			 DisMemberInfoModel disMemberInfo=disMemberInfoMapper.queryDisMemberInfoById(user_id);
			 if(disMemberInfo==null)
			 {
					responseData.setData(null);
					responseData.setMsg("用户不存在");
					responseData.setStatus("0");
					return responseData;
			 }
			 Map<String,String> paramMap=new HashMap<String, String>();
			 paramMap.put("userId", user_id);
			 paramMap.put("start", start+"");
			 paramMap.put("end", size+"");
			 paramMap.put("cardType", cardType);
			 paramMap.put("proxyTime",DateUtils.DateToStr(disMemberInfo.getDis_member_time(), "yyyy-MM-dd HH:mm:ss"));
			 if(flag!=null&&!"".equals(flag))
			 {
				 paramMap.put("flag", flag+"");
			 }
			 String result=HttpURLClient.post(paramMap, ip+"/api/saScoreFlow/partnerList");
			 System.out.println("每日收益访问的ip为:"+ip);
			 responseData.setData(JSONObject.parse(result));
			 responseData.setMsg("操作成功");
			 responseData.setStatus("1");
			 return responseData;
		}
		
		
		@Override
		public ResponseData queryMyEarningsDetail(String user_id, Integer start,
				Integer size, Integer flag,String creatTime,String cardType) {
			 DisMemberInfoModel disMemberInfo=disMemberInfoMapper.queryDisMemberInfoById(user_id);
			 if(disMemberInfo==null)
			 {
					responseData.setData(null);
					responseData.setMsg("用户不存在");
					responseData.setStatus("0");
					return responseData;
			 }
			 Map<String,String> paramMap=new HashMap<String, String>();
			 paramMap.put("userId", user_id);
			 paramMap.put("start", start+"");
			 paramMap.put("end", size+"");
			 paramMap.put("createTime",creatTime);
			 paramMap.put("cardType", cardType);
			 paramMap.put("proxyTime",DateUtils.DateToStr(disMemberInfo.getDis_member_time(), "yyyy-MM-dd HH:mm:ss"));
			 if(flag!=null&&!"".equals(flag))
			 {
				 paramMap.put("flag", flag+"");
			 }
			 String result=HttpURLClient.post(paramMap, ip+"/api/saScoreFlow/partnerDayList");
			 System.out.println("每日收益明细访问的ip为:"+ip);
			 responseData.setData(JSONObject.parse(result));
			 responseData.setMsg("操作成功");
			 responseData.setStatus("1");
			 return responseData;
		}

		@Override
		public ResponseData queryMermberNum(String user_id,Integer type) {
			 Map<String,Object> map=new HashMap<String, Object>();
			 map.put("user_id", user_id);
			 map.put("type", type);
			 List<Map<String,Object>> list=disMemberInfoMapper.queryMermberNum(map);
			 responseData.setData(list);
			 responseData.setMsg("操作成功");
			 responseData.setStatus("1");
			 return responseData;
		}
		
		@Override
		public ResponseData queryMermberNumTotal(String user_id) {
			 Map<String,Object> data=disMemberInfoMapper.queryMermberNumTotal(user_id);
			 responseData.setData(data);
			 responseData.setMsg("操作成功");
			 responseData.setStatus("1");
			 return responseData;
		}
}
